﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Net_SCADA
{
    class fdUsers
    {
        private static List<StringBuilder> _pwd = new List<StringBuilder>();
        public static int LoginIndex { get; set; } = 0;
        public static StringBuilder UserName { get; set; } = new StringBuilder();
        public static int UserRights { get; set; } = 0;
        public static List<StringBuilder> UsersName { get; private set; } = new List<StringBuilder>();

        public static List<int> Rights { get; } = new List<int>();

        public static string ManagerMD5 { get; set; }

        public static Boolean iniAddUser(StringBuilder sbUsername, StringBuilder sbPWD, int iRights)
        {
            string _filename = Application.StartupPath + "\\Settings\\Users.dat";
            if (File.Exists(_filename))
            {
                Ini _userinf = new Ini(_filename);
                _userinf.Write("Users", sbUsername.ToString(), MD5.GetMD5(sbPWD.ToString()) + "," + iRights.ToString());
                return true;
            }
            else
            {
                return false;
            }
        }

        public static Boolean iniDeleteUser(StringBuilder sbUsername)
        {
            string _filename = Application.StartupPath + "\\Settings\\Users.dat";
            if (File.Exists(_filename))
            {
                Ini _userinf = new Ini(_filename);
                _userinf.DeleteKey("Users", sbUsername.ToString());
                return true;
            }
            else
            {
                return false;
            }
        }

        public static Boolean iniChangePWD(StringBuilder sbUsername, StringBuilder sbPWD)
        {
            string _filename = Application.StartupPath + "\\Settings\\Users.dat";
            if (File.Exists(_filename))
            {
                Ini _userinf = new Ini(_filename);
                StringBuilder _info = new StringBuilder();
                _info.Append(_userinf.ReadValue("Users", sbUsername.ToString()));
                _userinf.Write("Users", sbUsername.ToString(), MD5.GetMD5(sbPWD.ToString()) + "," + _info.ToString().Substring(_info.ToString().IndexOf(",") + 1, 1));
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 加载所有用户的信息
        /// </summary>
        public static void iniLoadUses()
        {
            _pwd.Clear();
            Rights.Clear();
            UsersName.Clear();
            string _filename = Application.StartupPath + "\\Settings\\Users.dat";
            StringBuilder _info = new StringBuilder();
            if (File.Exists(_filename))
            {
                Ini _userinf = new Ini(_filename);
                UsersName = _userinf.GetKeys("Users");
                for (int i = 0; i < UsersName.Count; i++)
                {
                    _info.Clear();
                    _info.Append(_userinf.ReadValue("Users", UsersName[i].ToString()));
                    if (_info.ToString() != "" & _info.ToString().IndexOf(",") >= 0)
                    {
                        _pwd.Add(new StringBuilder().Append(_info.ToString().Substring(0, _info.ToString().IndexOf(","))));
                        Rights.Add(Convert.ToInt32(_info.ToString().Substring(_info.ToString().IndexOf(",") + 1, 1)));
                    }
                    else
                    {
                        UsersName.Remove(UsersName[i]);
                    }
                }
                ManagerMD5 = _userinf.ReadValue("Manager", "Manager");
            }
        }
        /// <summary>
        /// 注销用户
        /// </summary>
        public static void UserLogout()
        {
            UserName = new StringBuilder();
            UserRights = 0;
        }
        /// <summary>
        /// 检查sName用户名是否存在
        /// </summary>
        /// <param name="sName"></param>
        /// <returns></returns>
        public static bool iniCheckUsername(StringBuilder sName)
        {
            iniLoadUses();
            for (int i = 0; i < UsersName.Count; i++)
            {
                if (sName.ToString() == UsersName[i].ToString()) return true;
            }
            return false;
        }

        /// <summary>
        /// 检验管理员的密码是否正确
        /// </summary>
        /// <param name="sPWD"></param>
        /// <returns></returns>
        public static bool iniCheckManagerPWD(string sPWD)
        {
            string _filename = Application.StartupPath + "\\Settings\\Users.dat";
            if (File.Exists(_filename))
            {
                Ini _userinf = new Ini(_filename);
                ManagerMD5 = _userinf.ReadValue("Manager", "Manager");
                return MD5.GetMD5(sPWD) == ManagerMD5 ? true : false;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 进行用户密码的确认
        /// </summary>
        /// <param name="UserName">用户名</param>
        /// <param name="PWD">明文密码</param>
        /// <returns></returns>
        public static bool iniCheckUser(StringBuilder username, StringBuilder pwd)
        {//需要对PWD进行MD5的加密
            int _index = -1;// UsersName.IndexOf(username);
            for (int i = 0; i < UsersName.Count; i++)
            {
                if (username.Equals(UsersName[i]))
                {
                    _index = i;
                    break;
                }
            }
            if (_index != -1 && _index < UsersName.Count)
            {
                if (_pwd[_index].ToString()==MD5.GetMD5(pwd.ToString()))
                {
                    return true;
                }
                else
                {
                    UserName = new StringBuilder();
                    UserRights = 0;
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
    }
}
